/**
 * date：2010-11-11
 * author：eason
 * description：同时执行序列动作(_actList)的动作.
 * 该动作主要是针对一组可以同时执行的动作的操作.
 */
package com.pickgliss.action
{	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class SimultaneityQueueAction extends BaseAction
	{
		private var _actList : Array;
		private var _count : int;
				
		/**
		 * 构造函数. 
		 * @param actList 需执行的动作数组,成员需实现 IAction 接口.
		 * @param timeOut 超时时间.为0时表示不限时.
		 * 
		 */		
		public function SimultaneityQueueAction(actList : Array, timeOut : uint = 0)
		{
			_actList = actList;
			super(timeOut);
		}
		
		public override function act():void
		{	
			cancel();//先取消正在执行的动作.
			
			actAll();
			
			super.act();
		}
		private function actAll():void
		{
			var action : IAction;
			
			_count = 0;
			
			for(var i : int = 0; i < _actList.length; i++){
				action = _actList[i] as IAction;
				action.setCompleteFun(onItemActionComplete);
				action.act();
			}
		}
		
		/**
		 * 序列中的每一个动作执行完成的处理函数. 
		 * @param target
		 * 
		 */		
		private function onItemActionComplete(target : IAction):void
		{
			_count++;
			
			checkIsAllComplete();
		}
		private function checkIsAllComplete():void
		{
			if(_count >= _actList.length){
				_acting = false;				
				execComplete();
			}
		}
			
		/**
		 * 取消正在执行的动作. 
		 * 
		 */		
		override public function cancel():void
		{
			if(_acting){
				var action : IAction;
				for(var i : int = 0; i < _actList.length; i++){
					action = _actList[i] as IAction;
					action.cancel();
				}
				_acting = false;
			}
		}
		/**
		 * 获得动作序列。 
		 * @return 
		 * 
		 */
		public function getActions():Array
		{
			return _actList;
		}
	}
}